package org.jeecg.modules.app.v1.shop.entity;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.jeecg.modules.power.shop.entity.YlShopCategory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.io.Serializable;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 这里是一个注释
 *
 * @author scw
 * @version v1.0
 * @date
 */
@Data
public class ApiShopCategoryEntity implements Serializable {

    private static final Long serialVersionUID = 1L;

    private String id;
    private String name;
    private String pid;
    private String hasChild;
    private List<ApiShopCategoryEntity> children = new ArrayList<>();


    // 静态方法，用于从分类列表构建树形结构
    public static List<ApiShopCategoryEntity> buildTree(List<ApiShopCategoryEntity> categories, String rootId) {
        Map<String, ApiShopCategoryEntity> categoryMap = new HashMap<>();
        for (ApiShopCategoryEntity category : categories) {
            categoryMap.put(category.getId(), category);
        }
        List<ApiShopCategoryEntity> list = new ArrayList<>();
        for (ApiShopCategoryEntity category : categories) {
            if (category.getPid().equals(rootId)) {  // 假设rootId是顶级分类的pid
                ApiShopCategoryEntity root = category;
                buildTreeRecursive(root, categoryMap);
                list.add(root);
            }
        }
        return list;
    }

    // 递归辅助方法，用于构建树形结构
    private static void buildTreeRecursive(ApiShopCategoryEntity category, Map<String, ApiShopCategoryEntity> categoryMap) {
        for (ApiShopCategoryEntity potentialChild : categoryMap.values()) {
            if (potentialChild.getPid().equals(category.getId())) {
                category.getChildren().add(potentialChild);
                buildTreeRecursive(potentialChild, categoryMap);
            }
        }
    }

}
